package uk.org.mygrid.eobesity.csvserver.routes;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.HashMap;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.log4j.Logger;

/**
 * Base resource class that supports common behaviours or attributes shared by
 * all resources.
 *
 */
public class BaseResource {

    private static Logger logger = Logger.getLogger(BaseResource.class);
    private static HashMap<String, Boolean> completedJobList;
    private static HashMap<String, Boolean> completedSPSSList;
    private static HashMap<String, Boolean> completedStataList;

    /**
     * Returns the map of items managed by this application.
     *
     * @return the map of items managed by this application.
     */
    public static synchronized HashMap<String, Boolean> getCompletedJobList() {
        if (completedJobList == null) {
            logger.info("initialising completed job list");
            initJobs();
        }
        return completedJobList;
    }

    /**
     * Returns the map of items managed by this application.
     *
     * @return the map of items managed by this application.
     */
    public static synchronized HashMap<String, Boolean> getSPSSJobList() {
        if (completedSPSSList == null) {
            logger.info("initialising SPSS job list");
            initSPSS();
        }
        return completedSPSSList;
    }

    /**
     * Returns the map of items managed by this application.
     *
     * @return the map of items managed by this application.
     */
    public static synchronized HashMap<String, Boolean> getStataJobList() {
        if (completedStataList == null) {
            logger.info("initialising Stata job list");
            initStata();
        }
        return completedStataList;
    }

    private static void initJobs() {
        Configuration config = null;
        try {
            config = new PropertiesConfiguration("csv.properties");
        } catch (Exception e) {
            logger.error("Problem with service properties", e);

        }
        String serializeDirectory = config.getString("SerializeMap.0");
        File file = new File(serializeDirectory + "jobMap");
        try {
            FileInputStream stream = new FileInputStream(file);
            ObjectInputStream objectStream = new ObjectInputStream(stream);
            completedJobList = (HashMap<String, Boolean>) objectStream.readObject();
            objectStream.close();
        } catch (FileNotFoundException e) {
            logger.warn("Problem de-serializing job map, creating new one" + e);
            completedJobList = new HashMap<String, Boolean>();
        } catch (IOException e) {
            logger.warn("Problem de-serializing job map, creating new one " + e);
            completedJobList = new HashMap<String, Boolean>();
        } catch (ClassNotFoundException e) {
            logger.warn("Problem de-serializing job map, creating new one " + e);
            completedJobList = new HashMap<String, Boolean>();
        }
        File spssFile = new File(serializeDirectory + "spssJobMap");
        try {
            FileInputStream stream = new FileInputStream(spssFile);
            ObjectInputStream objectStream = new ObjectInputStream(stream);
            completedSPSSList = (HashMap<String, Boolean>) objectStream.readObject();
            objectStream.close();
        } catch (FileNotFoundException e) {
            logger.warn("Problem de-serializing job map, creating new one" + e);
            completedSPSSList = new HashMap<String, Boolean>();
        } catch (IOException e) {
            logger.warn("Problem de-serializing job map, creating new one " + e);
            completedSPSSList = new HashMap<String, Boolean>();
        } catch (ClassNotFoundException e) {
            logger.warn("Problem de-serializing job map, creating new one " + e);
            completedSPSSList = new HashMap<String, Boolean>();
        }

        File stataFile = new File(serializeDirectory + "stataJobMap");
        try {
            FileInputStream stream = new FileInputStream(stataFile);
            ObjectInputStream objectStream = new ObjectInputStream(stream);
            completedStataList = (HashMap<String, Boolean>) objectStream.readObject();
            objectStream.close();
        } catch (FileNotFoundException e) {
            logger.warn("Problem de-serializing job map, creating new one" + e);
            completedStataList = new HashMap<String, Boolean>();
        } catch (IOException e) {
            logger.warn("Problem de-serializing job map, creating new one " + e);
            completedStataList = new HashMap<String, Boolean>();
        } catch (ClassNotFoundException e) {
            logger.warn("Problem de-serializing job map, creating new one " + e);
            completedStataList = new HashMap<String, Boolean>();
        }
    }

    private static void initSPSS() {
        Configuration config = null;
        try {
            config = new PropertiesConfiguration("csv.properties");
        } catch (Exception e) {
            logger.error("Problem with service properties", e);

        }
        String serializeDirectory = config.getString("SerializeMap.0");

        File spssFile = new File(serializeDirectory + "spssJobMap");
        try {
            FileInputStream stream = new FileInputStream(spssFile);
            ObjectInputStream objectStream = new ObjectInputStream(stream);
            completedSPSSList = (HashMap<String, Boolean>) objectStream.readObject();
            objectStream.close();
        } catch (FileNotFoundException e) {
            logger.warn("Problem de-serializing SPSS map, creating new one" + e);
            completedSPSSList = new HashMap<String, Boolean>();
        } catch (IOException e) {
            logger.warn("Problem de-serializing SPSS map, creating new one " + e);
            completedSPSSList = new HashMap<String, Boolean>();
        } catch (ClassNotFoundException e) {
            logger.warn("Problem de-serializing SPSS map, creating new one " + e);
            completedSPSSList = new HashMap<String, Boolean>();
        }
    }

    private static void initStata() {
        Configuration config = null;
        try {
            config = new PropertiesConfiguration("csv.properties");
        } catch (Exception e) {
            logger.error("Problem with service properties", e);

        }
        String serializeDirectory = config.getString("SerializeMap.0");

        File stataFile = new File(serializeDirectory + "stataJobMap");
        try {
            FileInputStream stream = new FileInputStream(stataFile);
            ObjectInputStream objectStream = new ObjectInputStream(stream);
            completedStataList = (HashMap<String, Boolean>) objectStream.readObject();
            objectStream.close();
        } catch (FileNotFoundException e) {
            logger.warn("Problem de-serializing Stata map, creating new one" + e);
            completedStataList = new HashMap<String, Boolean>();
        } catch (IOException e) {
            logger.warn("Problem de-serializing Stata map, creating new one " + e);
            completedStataList = new HashMap<String, Boolean>();
        } catch (ClassNotFoundException e) {
            logger.warn("Problem de-serializing Stata map, creating new one " + e);
            completedStataList = new HashMap<String, Boolean>();
        }
    }
}
